﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Club
{
    public class Club_Wear : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            string cIdString = context.Request.QueryString["ClubId"];
            if (!string.IsNullOrEmpty(cIdString))
            {
                int cId = int.Parse(cIdString);
                Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Jy123.Model.Club_Main club = BLLClub.GetModel(cId);
                if (!string.IsNullOrEmpty(type))
                {
                    string html = context.Cache["clubwear"].ToString();
                    string strObject = context.Cache["flash"].ToString();
                    //string strXML = string.Format("/Module/Club/ReadyForBattle.ashx?ClubId={0}&Guid={1}", cIdString,Guid.NewGuid());
                    string strXML = string.Format("/Module/Club/XML/{0}/{1}.xml", cIdString,Guid.NewGuid());
                    strObject = strObject.Replace("$XML$", strXML);
                    //strObject = strObject.Replace("$XML$", "/Cache/Module/Flash/test.xml");
                    strObject = strObject.Replace("$Value$", "/Cache/Module/Flash/Court.swf");
                    html = html.Replace("$Object$", strObject);
                    context.Response.Write(html);
                }
                else
                {
                    GetXML(context, cIdString);
                }
            }
        }

        private void GetXML(HttpContext context, string strClubId)
        {
            string strResult = context.Cache["readyforbattle"].ToString();
            if (!string.IsNullOrEmpty(strClubId))
            {
                int clubId = int.Parse(strClubId);
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Model.Club_Main club = BLLClub.GetModel(clubId);
                BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                Model.User_Main user = BLLUser.GetModel(club.FUser.Value);
                BLL.Props.Props BLLRProps = new Jy123.BLL.Props.Props();
                List<Model.Club_PropsRecord> listRecord = BLLRProps.GetPropsRecord(user.ID, club.FJType.Value, "1");
                Dictionary<int, Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();

                List<Model.Public_Props> listProps = BLLRProps.GetProps(club.FJType.Value, "1");
                foreach (Model.Public_Props cProps in listProps)
                {
                    dicProps.Add(cProps.ID, cProps);
                }
                StringBuilder sbString = new StringBuilder();
                foreach (Model.Club_PropsRecord record in listRecord)
                {
                    string strPart = context.Cache["xmlvenuespart"].ToString();
                    Model.Public_Props cProps = dicProps[record.FProps.Value];
                    strPart = strPart.Replace("$PartId$", cProps.ID.ToString());
                    strPart = strPart.Replace("$DisplayName$", cProps.PName);
                    strPart = strPart.Replace("$DisplayOrder$", cProps.DisplayOrder.ToString());
                    string isShow = "0";
                    if (record.IsUsed)
                    {
                        isShow = "1";
                    }
                    strPart = strPart.Replace("$IsShow$", isShow);
                    strPart = strPart.Replace("$ImageUrl$", cProps.Model);
                    strPart = strPart.Replace("$PosX$", cProps.PosX.ToString());
                    strPart = strPart.Replace("$PosY$", cProps.PosY.ToString());
                    strPart = strPart.Replace("$BtnImageUrl$", cProps.SmallModel);
                    sbString.Append(strPart);
                }
                string backImg = "/Cache/Module/Flash/CourtImages/ReadyForBattle.png";
                //需要改进，暂时写死
                if (club.FJType == 7)
                {
                    backImg = "/Cache/Module/Flash/CourtImages/ReadyForFb.jpg";
                }
                strResult = strResult.Replace("$BackImg$", backImg);
                strResult = strResult.Replace("$Part$", sbString.ToString());
            }
            strResult = strResult.Replace("$Part$", string.Empty);
            byte[] byBuffer = System.Text.Encoding.Default.GetBytes(strResult);
            context.Response.BinaryWrite(byBuffer);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
